<!-- 渠道信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="渠道名称">
                            <a-input v-model="channelName" allow-clear placeholder="请输入渠道名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="渠道标识">
                            <a-input v-model="channelTag" allow-clear placeholder="请输入渠道标识" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size,}" size="small" :scroll="{ x: 1500, }">
            <div slot="creditSupportBank" slot-scope="creditSupportBank">
                <ellipsis :length="18" tooltip>{{ creditSupportBank }}</ellipsis>
            </div>
            <div slot="creditNotSupportBank" slot-scope="creditNotSupportBank">
                <ellipsis :length="18" tooltip>{{ creditNotSupportBank }}</ellipsis>
            </div>
            <div slot="debitSupportBank" slot-scope="debitSupportBank">
                <ellipsis :length="18" tooltip>{{ debitSupportBank }}</ellipsis>
            </div>
            <div slot="debitNotSupportBank" slot-scope="debitNotSupportBank">
                <ellipsis :length="18" tooltip>{{ debitNotSupportBank }}</ellipsis>
            </div>
            <div slot="openOnOff" slot-scope="openOnOff">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="openOnOff == '1' ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button class="errorColor" type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改渠道信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="渠道名称">
                        <a-input v-model="item.name" placeholder="请输入渠道名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="渠道标识">
                        <a-input v-model="item.channelTag"  placeholder="请输入渠道标识" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔最小金额">
                        <a-input v-model="item.minAmount" placeholder="请输入单笔最小金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔最小金额">
                        <a-input v-model="item.maxAmount" placeholder="请输入单笔最大金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="信用卡支持银行" help="支持银行，用 - 分隔">
                        <a-input v-model="item.creditSupportBank" type="textarea" placeholder="请输入信用卡支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="信用卡不支持银行" help="不支持银行，用 - 分隔">
                        <a-input v-model="item.creditNotSupportBank" type="textarea" placeholder="请输入信用卡不支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="储蓄卡支持银行" help="支持银行，用 - 分隔">
                        <a-input v-model="item.debitSupportBank" type="textarea" placeholder="请输入储蓄卡支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="储蓄卡不支持银行" help="不支持银行，用 - 分隔">
                        <a-input v-model="item.debitNotSupportBank" type="textarea" placeholder="请输入储蓄卡不支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="优先级" help="越大越优先,默认为0">
                        <a-input v-model="item.priority" placeholder="请输入渠道优先级" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.openOnOff" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryQuickChannelConfig , upadteQuickChannelConfig } from '@/api/product';
import { Ellipsis } from '@/components'
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            brandId: this.global.brandId,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            channelName: '', // 渠道名称
            channelTag: '', // 渠道标识
            statusList: [
                { id: 1, name: '开启' },
                { id: 0, name: '关闭' },
            ],
            columns,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: true,
            visible: false,
            item: {
                'id': '',
                'name': '', // 渠道名称
                'channelTag': '', // 渠道标识
                'minAmount': '', // 单笔最小金额	
                'maxAmount': '', // 单笔最大金额		
                'creditSupportBank': '', // 信用卡支持银行,用 - 分隔
                'creditNotSupportBank': '', // 信用卡不支持银行,用 - 分隔
                'debitSupportBank': '', // 储蓄卡支持银行,用 - 分隔
                'debitNotSupportBank': '', // 储蓄卡不支持银行,用 - 分隔
                'priority': '', // 优先级,越大越优先,默认为0
                'openOnOff': '1', // 状态，1：可用，0：不可用
            },
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询渠道配置
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'name':_this.channelName,
                'channelTag':_this.channelTag,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryQuickChannelConfig(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.name = '';
            this.item.channelTag = '';
            this.item.creditSupportBank = '';
            this.item.creditNotSupportBank = '';
            this.item.debitSupportBank = '';
            this.item.debitNotSupportBank = '';
            this.item.priority = '';
            this.item.minAmount = '';
            this.item.maxAmount = '';
            this.item.openOnOff = 1
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.isAdd = false;
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.name == ''){
                _this.$message.warning('请输入渠道名称')
                return;
            }
            if(_this.item.channelTag == ''){
                _this.$message.warning('请输入渠道标识')
                return;
            }
            if(_this.item.minAmount == ''){
                _this.$message.warning('单笔最小金额')
                return;
            }
            if(_this.item.maxAmount == ''){
                _this.$message.warning('单笔最大金额')
                return;
            }
            if(_this.item.creditSupportBank == ''){
                _this.$message.warning('信用卡支持银行,用 - 分隔')
                return;
            }
            if(_this.item.debitSupportBank == ''){
                _this.$message.warning('储蓄卡支持银行,用 - 分隔')
                return;
            }
            const params = {
                'id': _this.item.id,
                'name': _this.item.name,
                'channelTag': _this.item.channelTag,
                'openOnOff': _this.item.openOnOff,
                'minAmount': _this.item.minAmount,
                'maxAmount': _this.item.maxAmount,
                'creditSupportBank': _this.item.creditSupportBank,
                'creditNotSupportBank': _this.item.creditNotSupportBank,
                'debitSupportBank': _this.item.debitSupportBank,
                'debitNotSupportBank': _this.item.debitNotSupportBank,
                'priority': _this.item.priority,
            }
            upadteQuickChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'isDelete': '1'
            }
            upadteQuickChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>
</style>